package cn.tedu.mall.seckill.consumer;

import cn.tedu.mall.pojo.seckill.model.SeckillSuccess;
import cn.tedu.mall.seckill.config.RabbitMqComponentConfiguration;
import cn.tedu.mall.seckill.mapper.SeckillSkuMapper;
import cn.tedu.mall.seckill.mapper.SeckillSuccessMapper;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RabbitListener(queues = RabbitMqComponentConfiguration.SECKILL_QUEUE)
public class RabbitConsumer {

    @Autowired
    private SeckillSkuMapper seckillSkuMapper;

    @Autowired
    private SeckillSuccessMapper seckillSuccessMapper;


    @RabbitHandler
    @Transactional
    public void process(SeckillSuccess seckillSuccess){
        //同步库存数
        seckillSkuMapper.updateReduceStockBySkuId(seckillSuccess.getSkuId(),seckillSuccess.getQuantity());
        //秒杀信息入库
        seckillSuccessMapper.insertSuccess(seckillSuccess);
    }

}
